/**
 * 卡片分类管理
 */
var React = require("react");
var {BootstrapTable,TableHeaderColumn} = require("react-bootstrap-table");
var {Button} = require("react-bootstrap");
var {ListPage,Util} = require("flower");
class Index extends ListPage{

    constructor(props){
        super(props);
        this.state = {

        };
        this.selectRow={
            mode:'checkbox',
            onSelect: this.handleRowSelect,
            onSelectAll:this.handleRowSelectAll
        }
        this.options = {
            paginationShowsTotal: Util.renderPaginationShowsTotal
        };
    }

    handleRowSelect(row, isSelected, e) {
        console.info(row);
        console.info(isSelected);
        console.info(e);
    }

    handleRowSelectAll(isSelected, rows) {
        console.info(isSelected);
        console.info(rows);
    }
    //加载数据
    loadData(){
        return Util.get("/sort/list");
    }


    optionFormat(cell,row){
        return <span>
            <Button bsStyle="success" bsSize="xsmall" style={{marginRight:"10px"}} onClick={()=>{
                Util.goto("/load/admin/sort/Add.jsx",{dataId:row.id});
            }}>编辑</Button>
            <Button bsStyle="danger" bsSize="xsmall" onClick={()=>{
                if(confirm("真的要删除["+row.name+"]分类吗?")){
                    var param = {
                        id:row.id
                    }
                    //1.先判断该分类下有无卡片，有卡片提示先去删掉卡片，才能删分类
                    var ret = Util.get("/sort/gridNum",param);
                    if(ret>0){
                        alert("该分类下包含"+ret+"张卡片，想删除该分类，请先删除该分类下的卡片！");
                    }else{
                        var retJson = Util.get("/sort/delete",param);
                        if(retJson.state == 200){
                            alert("删除成功!");
                            Util.goto("/load/admin/sort/Index.jsx");
                        }else{
                            alert("删除失败！原因:"+retJson.msg);
                        }
                    }
                }

            }}>删除</Button>
        </span>
    }

    title(){
        var data = [
            {name:"首页",href:"/show/admin/Main.jsx"},
            {name:"分类管理",href:"/load/admin/sort/Index.jsx"}
        ];
        return this.cbb(data);
    }

    onAdd(){
        Util.goto("/load/admin/sort/Add.jsx");
    }
    option(){
       return <span>
                <Button bsStyle="primary" bsSize="small" onClick={this.onAdd.bind(this)}>新增</Button>
           </span>

    }

    content(){
        return <BootstrapTable data={this.loadData()} striped condensed hover pagination
                               headerStyle={{textAlign:"center"}}
                               options={this.options}
                               //selectRow={ this.selectRow }
                               search = {true}
        >
            <TableHeaderColumn hidden isKey dataField='id'>ID</TableHeaderColumn>
            <TableHeaderColumn dataField='name' dataSort={true}>分类名称</TableHeaderColumn>
            <TableHeaderColumn dataField='sortOrder' width="60px" dataAlign="center" dataSort={true}>排序</TableHeaderColumn>
            <TableHeaderColumn dataField='creator' dataAlign="center" width="80px">创建人</TableHeaderColumn>
            <TableHeaderColumn dataField='createTime' dataAlign="center" dataFormat={ Util.timeFormat }>创建时间</TableHeaderColumn>
            <TableHeaderColumn dataField='editor' dataAlign="center" width="80px">修改人</TableHeaderColumn>
            <TableHeaderColumn dataField='lastEditTime' dataAlign="center"  dataFormat={ Util.timeFormat }>最后修改时间</TableHeaderColumn>
            <TableHeaderColumn dataField='option' dataAlign="center" width="100px" dataFormat={ this.optionFormat }>操作</TableHeaderColumn>
        </BootstrapTable>
    }
}
module.exports = Index;